//
//  userInfoView.m
//  QRMaster
//
//  Created by myApple on 15/4/27.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "StaffInfoView.h"
#import "LineView.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import "UIImageView+WebCache.h"
#import "TouchBtnColor.h"
#import "StaffManagerViewController.h"
#import "JudgeRole.h"
#import "StaffLogic.h"
#import "CommunityInfoLogic.h"
#import "BackBtn.h"

#define fontSize [UIFont systemFontOfSize:14.0]
#define ViewHeigth  50

@interface StaffInfoView ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
{
    
    UIScrollView * InfoScrollView;
    UIImageView * TopBackGroundView;
    UILabel * userNameLabel;
    
    UILabel * sexLabel;
    UILabel * telePhoenLabel;
    UILabel * roleLabel;
    UIImageView * roleImgView;
    UILabel * emaliLabel;
    
    BOOL editFlag;
    
//    UIView * editingView;//编辑时候显示的view
    UIScrollView * editingView;
    UITextField * userNameTextField;
    UIButton * sexChangeBtn;
    UILabel * editingRoleLabel;
    UIView * editingRoleView;
    UIButton * positionBtn;//切换职务
    UIButton * dutyBtn;//切换职责
    UIButton * editingRoomBtn;
    UIButton * editingAccessBtn;
    UIButton * editingTimeBtn;
    
    
    UIButton * sexBtn;
    UIImageView * titleImgView;//个人头像图
    
    NSArray * positionArray;
    NSArray * dutyArray;
    NSArray * roleArray;
    
    UITableView * listTableView;
    
    UIButton * roomBtn;
    UIButton * accessBtn;
    UIButton * timeBtn;
    UIButton * roleBtn;
    
    
    UIButton * manBtn;
    UIButton * womanBtn;
    UIButton * callBtn;
    NSString * sexFlag;
    
    NSMutableDictionary * saveDict;    
    UIImageView * womanView;
    UIImageView * manView;
    
    NSString * roleIndex;
    
    UIImageView * editImgView;
    UIView * editBackView;//点击事件的背景

    BOOL staffPower;
    
    
    //多语言
}

@end


@implementation StaffInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self prepareHeadView];
        [self prepareScrollView];
        [self prepareEditingView];
        [self prepareListView];
         staffPower = NO;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
        editFlag = YES;
       
    }
    return self;
}

-(void)prepareHeadView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = 64;
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    topView.backgroundColor = [UIColor blackColor];
    [self addSubview:topView];
    
    y = 0;
    width = ScreenWidth;
    height = ScreenHeight / 3.0;
    TopBackGroundView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    TopBackGroundView.userInteractionEnabled = YES;
    TopBackGroundView.image = [UIImage imageNamed:@"个人背景"];
    [self addSubview:TopBackGroundView];
    
    
    UIView * alpBackView =[[UIView alloc]initWithFrame:topView.frame];
    alpBackView.backgroundColor =[UIColor clearColor];
    [self addSubview:alpBackView];
    
    x = 10;
    y = 22;
    height = 40;
    width = 40;
    UIButton * backBtn = [BackBtn initWithFrame:CGRectMake(x, y, width, height) withImage:@"24x24"];
    backBtn.frame = CGRectMake(x, y, width, height);
    [backBtn addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    [alpBackView addSubview:backBtn];
    
    //编辑
    x = ScreenWidth - 60;
    width = 40;
    y = 22;
    height = 40;
    editBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [alpBackView addSubview:editBackView];
    x = 0;
    width = 20;
    height = 20;
    y = 10;
    editImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editImgView.image = [UIImage imageNamed:@"ic_edit"];
    editImgView.userInteractionEnabled = NO;
    [editBackView addSubview:editImgView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editClick)];
    [editBackView addGestureRecognizer:editTap];
}

#pragma mark -- 点击编辑按钮
-(void)editClick
{
    if (editFlag) {
        editImgView.image = [UIImage imageNamed:@"ic_save"];
        CGRect frame = InfoScrollView.frame;
        CGRect ImgFrame = TopBackGroundView.frame;
        CGRect HeadImgFrame = titleImgView.frame;
        frame.origin.y = 64;
        ImgFrame.size.height = 0;
        HeadImgFrame.origin.y = 34;
        [UIView animateWithDuration:0.3 animations:^{
            InfoScrollView.frame = frame;
            TopBackGroundView.frame = ImgFrame;
            titleImgView.frame = HeadImgFrame;
            titleImgView.alpha = 0.0;
        } completion:^(BOOL finished) {
            InfoScrollView.hidden = YES;
            editingView.hidden = NO;
            [userNameTextField becomeFirstResponder];
        }];
    }
    else
    {
        [self makeSureSave];
       
    }
    editFlag = !editFlag;
    
}

-(void)makeSureSave
{
    [self tapClick];
    if (userNameTextField.text.length > 20||userNameTextField.text.length < 1)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return;
    }
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    [saveDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [saveDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [saveDict setValue:userNameTextField.text forKey:@"name"];
    [saveDict setValue:roleIndex forKey:@"role"];
    [saveDict setValue:sexFlag forKey:@"gender"];
    [saveDict setValue:_communitID forKey:@"communityId"];
    [saveDict setValue:_model.userID forKey:@"memberId"];
    
    if([self getTimestamp:_communitID timeKey:@"member_timestamp"] != nil)
      {
        [saveDict setValue:[self getTimestamp:_communitID timeKey:@"member_timestamp"] forKey:@"timestamp"];
      }
    else
      {
        [saveDict setValue:@"" forKey:@"timestamp"];
      }
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(EDITADDINFO) params:saveDict block:^(id downLoadData, id error) {
        NSDictionary * cache = [downLoadData objectForKey:@"cache"];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if(cache != nil)
        {
            if([@"0" isEqualToString:[NSString stringWithFormat:@"%@",[cache objectForKey:@"status"]]]==YES)
            {
                [self updateTimestamp:_communitID timeKey:@"member_timestamp" timeKeyValue:[cache objectForKey:@"timestamp"]];
            }
            
            if (data != nil)
            {
                
                [PromptInfoView showMessage:NSLocalizedString(@"editSuccess", nil)];
                editImgView.image = [UIImage imageNamed:@"ic_edit"];
                InfoScrollView.hidden = NO;
                editingView.hidden = YES;
                CGRect frame = InfoScrollView.frame;
                CGRect ImgFrame = TopBackGroundView.frame;
                CGRect HeadImgFrame = titleImgView.frame;
                frame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0;
                ImgFrame.size.height = [UIScreen mainScreen].bounds.size.height / 3.0;
                HeadImgFrame.origin.y = [UIScreen mainScreen].bounds.size.height / 3.0 - 30;
                [UIView animateWithDuration:0.3 animations:^{
                    InfoScrollView.frame = frame;
                    TopBackGroundView.frame = ImgFrame;
                    titleImgView.frame = HeadImgFrame;
                    titleImgView.alpha = 1.0;
                } completion:^(BOOL finished) {
                    InfoScrollView.scrollEnabled = YES;
                }];
                
                userNameLabel.text = userNameTextField.text;
                roleLabel.text = editingRoleLabel.text;
                sexLabel.text = [sexFlag isEqualToString:@"1"]?NSLocalizedString(@"man", nil):NSLocalizedString(@"woman", nil);
                [userNameTextField resignFirstResponder];
                [self LookUserPower];
                _model.userName = userNameTextField.text;
                _model.sex = sexFlag;
                _model.StaffRole = roleIndex;
                StaffLogic * staffDataBase = [[StaffLogic alloc]init];
                [staffDataBase updateStaffData:_model communityId:_communitID];
            }
            
        }
        else
        {
            
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }

    }];
}


-(void)prepareScrollView
{
    float ScreenWidth = self.frame.size.width;
    float ScreenHeight = self.frame.size.height;
    float x = 0;
    float y = TopBackGroundView.frame.size.height;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    InfoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    InfoScrollView.showsVerticalScrollIndicator = YES;
    InfoScrollView.showsHorizontalScrollIndicator = NO;
    InfoScrollView.backgroundColor = RGBA(237, 238, 241, 1);
    [self addSubview:InfoScrollView];
    
    //姓名
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = 50;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:nameView];
    
    x = 30;
    y = 25;
    width = nameView.frame.size.width - x;
    height = 20;
    userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameLabel.font = [UIFont systemFontOfSize:14.0];
    [nameView addSubview:userNameLabel];
    //个人头像
    x = 20;
    y = TopBackGroundView.frame.size.height - 30;
    width = 50;
    height = 50;
    titleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    titleImgView.layer.cornerRadius = 25;
    titleImgView.layer.masksToBounds =YES;
    [self addSubview:titleImgView];
    
    //性别
    x = 10;
    y = 5 + nameView.frame.size.height + nameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:sexView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * sexTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexTextLabel.text = NSLocalizedString(@"sex", nil);
    sexTextLabel.font = fontSize;
    [sexView addSubview:sexTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexLabel.font = fontSize;
    sexLabel.textColor = RGBA(143, 147, 148, 1);
    [sexView addSubview:sexLabel];
    
    //角色
    x = 10;
    y = 5 + sexView.frame.size.height + sexView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * roleView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roleView.backgroundColor = RGBA(248, 248, 248, 1);
    [InfoScrollView addSubview:roleView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * roleTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roleTextLabel.text = NSLocalizedString(@"roleId", nil);
    roleTextLabel.font = fontSize;
    [roleView addSubview:roleTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    roleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roleLabel.font = fontSize;
    roleLabel.textColor = RGBA(143, 147, 148, 1);
    [roleView addSubview:roleLabel];
    
    //手机号
    x = 10;
    y = 5 + roleView.frame.size.height + roleView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIButton * telePhoneBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [telePhoneBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [telePhoneBtn addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:telePhoneBtn.frame.size] forState:UIControlStateHighlighted];
    [InfoScrollView addSubview:telePhoneBtn];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * telePhoneTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneTextLabel.text = NSLocalizedString(@"telePhoneNum", nil);
    telePhoneTextLabel.font = fontSize;
    [telePhoneBtn addSubview:telePhoneTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    telePhoenLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoenLabel.font = fontSize;
    [telePhoneBtn addSubview:telePhoenLabel];
    
    x = telePhoneBtn.frame.size.width - 50;
    y = 0;
    height = 50;
    width = 50;
    callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    callBtn.frame = CGRectMake(x, y, width, height);
    [callBtn setBackgroundColor:RGBA(125, 194, 52, 1)];
    [callBtn addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneBtn addSubview:callBtn];
    
    
    x = 15;
    y = 15;
    width = 20;
    height = 20;
    UIImageView * callImg = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    callImg.image = [UIImage imageNamed:@"ic_call30"];
    [callBtn addSubview:callImg];
    
    //邮箱
    x = 10;
    y = 5 + telePhoneBtn.frame.size.height + telePhoneBtn.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * emaliView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emaliView.backgroundColor = RGBA(248, 248, 248, 1);
    [telePhoneBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [InfoScrollView addSubview:emaliView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * emailTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emailTextLabel.text = NSLocalizedString(@"email", nil);
    emailTextLabel.font = fontSize;
    [emaliView addSubview:emailTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    emaliLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    emaliLabel.font = fontSize;
    [emaliView addSubview:emaliLabel];
    
    //开门权限
    x = 10;
    y = emaliView.frame.size.height + emaliView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    roomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roomBtn.frame = CGRectMake(x, y, width, height);
    roomBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [roomBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:roomBtn.frame.size] forState:UIControlStateHighlighted];
    [roomBtn addTarget:self action:@selector(roomSelect) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:roomBtn];
    
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.text = NSLocalizedString(@"openRoomPower", nil);
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [roomBtn addSubview:roomLabel];
    
    
    x = roomBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * roomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomImgView.image = [UIImage imageNamed:@"36右箭头"];
    [roomBtn addSubview:roomImgView];
    
    //时间周期属性
    x = roomBtn.frame.origin.x;
    y = roomBtn.frame.origin.y + roomBtn.frame.size.height + 5;
    width = roomBtn.frame.size.width;
    height = ViewHeigth;
    timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.frame = CGRectMake(x, y, width, height);
    timeBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [timeBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:timeBtn.frame.size] forState:UIControlStateHighlighted];
    [timeBtn addTarget:self action:@selector(modifyTimeClick) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:timeBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 120;
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeLabel.text = NSLocalizedString(@"setOpenTime", nil);
    timeLabel.font = [UIFont systemFontOfSize:14.0];
    [timeBtn addSubview:timeLabel];
    
    
    x = timeBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * timeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeImgView.image = [UIImage imageNamed:@"36右箭头"];
    [timeBtn addSubview:timeImgView];
    
    //管理权限
    x = timeBtn.frame.origin.x;
    y = timeBtn.frame.origin.y + timeBtn.frame.size.height + 5;
    width = timeBtn.frame.size.width;
    height = ViewHeigth;
    accessBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    accessBtn.frame = CGRectMake(x, y, width, height);
    accessBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [accessBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:accessBtn.frame.size] forState:UIControlStateHighlighted];
    [accessBtn addTarget:self action:@selector(accessClick) forControlEvents:UIControlEventTouchUpInside];
    [InfoScrollView addSubview:accessBtn];
    
    x = 10;
    y = 15;
    height = 20;
    width = 180;
    UILabel * accessLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    accessLabel.text = NSLocalizedString(@"managerPower", nil);
    accessLabel.font = [UIFont systemFontOfSize:14.0];
    [accessBtn addSubview:accessLabel];
    
    x = accessBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * accessImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    accessImgView.image = [UIImage imageNamed:@"36右箭头"];
    [accessBtn addSubview:accessImgView];
    InfoScrollView.contentSize = CGSizeMake(0, accessBtn.frame.origin.y + accessBtn.frame.size.height);
}

#pragma mark -- 点击拨打手机

-(void)callPhone
{
    NSString * callString = nil;
    if (![_model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
        callString = [NSString stringWithFormat:@"+%@-%@",_model.areaCode,_model.mobile];
    }
    else
    {
        callString = telePhoenLabel.text;
    }
    NSString *telUrl = [NSString stringWithFormat:@"telprompt://%@",callString];
    NSURL *url = [NSURL URLWithString:telUrl];
    [[UIApplication sharedApplication] openURL:url];
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 64;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editingView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingView.backgroundColor = RGBA(237, 238, 241, 1);
    editingView.hidden = YES;
    [self addSubview:editingView];
    
    //姓名
    x = 10;
    y = 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:nameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * editUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editUserNameLabel.text = NSLocalizedString(@"userName", nil);
    editUserNameLabel.font = fontSize;
    [nameView addSubview:editUserNameLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameTextField.font = [UIFont systemFontOfSize:14.0];
    userNameTextField.borderStyle = UITextBorderStyleNone;
    userNameTextField.delegate = self;
    [nameView addSubview:userNameTextField];
    
    //性别
    x = 10;
    y = 5 + nameView.frame.size.height + nameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [sexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text = NSLocalizedString(@"man", nil);
    manLabel.font = fontSize;
    [sexView addSubview:manLabel];
    
    x = 180;
    y = 0;
    width = 40;
    height = ViewHeigth;
    womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 50, height)];
    womanLabel.text = NSLocalizedString(@"woman", nil);
    womanLabel.font = fontSize;
    [sexView addSubview:womanLabel];
    
    //角色
    x = 10;
    y = 5 + sexView.frame.size.height + sexView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    editingRoleView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingRoleView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:editingRoleView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * editingTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingTextLabel.text = NSLocalizedString(@"roleId", nil);
    editingTextLabel.font = fontSize;
    [editingRoleView addSubview:editingTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    editingRoleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingRoleLabel.font = fontSize;
    editingRoleLabel.text = NSLocalizedString(@"general", nil);
    [editingRoleView addSubview:editingRoleLabel];
    
    roleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roleBtn.frame = CGRectMake(editingRoleView.frame.size.width - 50, 0, 50, 50);
    roleBtn.backgroundColor = [UIColor greenColor];
    [roleBtn addTarget:self action:@selector(roleClick:) forControlEvents:UIControlEventTouchUpInside];
    [editingRoleView addSubview:roleBtn];
    
    roleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(editingRoleView.frame.size.width - 35, 15, 20, 20)];
    UIImage * roleImgage = [UIImage imageNamed:@"36展开向下"];
    roleImgView.image = roleImgage;
    [editingRoleView addSubview:roleImgView];
    
}


-(void)tapClick
{
    [userNameTextField resignFirstResponder];
}

-(void)manClick
{
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
    
}

-(void)womanClick
{
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    
}


-(void)prepareListView
{
    listTableView =[[UITableView alloc]init];
    listTableView.delegate = self;
    listTableView.dataSource = self;
    listTableView.hidden = YES;
    listTableView.backgroundColor = RGBA(237, 238, 241, 1);
    [editingView addSubview:listTableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return roleArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    if (roleArray.count > indexPath.row) {
        cell.textLabel.text = roleArray[indexPath.row];
    }
        return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingRoleLabel.text = roleArray[indexPath.row];
    if (indexPath.row == 0) {
        roleIndex = [NSString stringWithFormat:@"%d",(int)indexPath.row + 3];
    }
    else
    {
        roleIndex = [NSString stringWithFormat:@"%d",(int)indexPath.row + 1];
    }
    
    listTableView.hidden = YES;
}


-(void)accessClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(userInfoViewDelegateSend:)]) {
        [_delegate userInfoViewDelegateSend:@"manager"];
    }
}

-(void)roomSelect
{
    if (_delegate && [_delegate respondsToSelector:@selector(userInfoViewDelegateSend:)]) {
        [_delegate userInfoViewDelegateSend:@"room"];
    }
}

-(void)modifyTimeClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(userInfoViewDelegateSend:)]) {
        [_delegate userInfoViewDelegateSend:@"time"];
    }
}

-(void)roleClick:(UIButton *)btn
{
    [self tapClick];//解除第一响应
    float x = editingRoleView.frame.origin.x;
    float y = editingRoleView.frame.size.height + editingRoleView.frame.origin.y;
    float width = [UIScreen mainScreen].bounds.size.width - x - 20;
    float height = 80;
    
    listTableView.frame = CGRectMake(x, y, width, height);
    if (listTableView.hidden) {
        listTableView.hidden = NO;
    }
    else
        listTableView.hidden = YES;
    [listTableView reloadData];

}


-(void)setModel:(StaffModel *)model
{
    _model = model;
    if ([_model.StaffRole integerValue] == 2) {
        roleIndex = @"2";
        editingRoleLabel.text = roleArray[1];
        roleLabel.text = roleArray[1];
    }
    else if([_model.StaffRole integerValue] == 1)
    {
        roleLabel.text = NSLocalizedString(@"manager", nil);
    }
    else
    {
        roleIndex = @"3";
        editingRoleLabel.text = roleArray[0];
        roleLabel.text = roleArray[0];
    }
    userNameTextField.text = model.userName;
    userNameLabel.text = model.userName;
    emaliLabel.text = model.email;
    if (model.email == nil || [model.email isEqualToString:@""]) {
        if (![model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
            telePhoenLabel.text = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile] ;
        }
        else
        {
            telePhoenLabel.text = model.mobile;
        }
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",HEADERURL,model.headUrl];
    [titleImgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"96人像"]];
    if ([model.sex isEqualToString:@"1"]) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
    else
    {
        womanBtn.selected = YES;
        manBtn.selected = NO;
        sexFlag = @"0";
    }
    if ([telePhoenLabel.text isEqualToString:@""]|| telePhoenLabel.text == nil) {
        callBtn.hidden = YES;
    }
    sexLabel.text = ([sexFlag isEqualToString:@"0"]?NSLocalizedString(@"woman", nil):NSLocalizedString(@"man", nil));
    [self LookUserPower];
}

-(void)setCommunitID:(NSString *)communitID
{
    _communitID = communitID;
    saveDict = [[NSMutableDictionary alloc]init];
    roleArray = @[NSLocalizedString(@"general", nil),NSLocalizedString(@"groupManagement", nil)];
}

#pragma mark -- 网络请求来查看编辑人的权限
-(void)LookUserPower
{
    roomBtn.hidden = YES;
    timeBtn.hidden = YES;
    accessBtn.hidden = YES;
    editBackView.userInteractionEnabled = NO;
    InfoScrollView.contentSize = CGSizeMake(0, accessBtn.frame.origin.y + accessBtn.frame.size.height);
    NSInteger rolePower = [JudgeRole judgeRoleShowStaffInfo:[_model.StaffRole integerValue] withRole:_role];
    switch (rolePower) {
        case 0:
            editBackView.hidden = YES;
            InfoScrollView.contentSize = CGSizeMake(0, roomBtn.frame.origin.y);
            break;
        case 1:
            roomBtn.hidden = NO;
            timeBtn.hidden = NO;
            accessBtn.hidden = NO;
            editBackView.userInteractionEnabled = YES;
            break;
        case 2:
            roomBtn.hidden = NO;
            timeBtn.hidden = NO;
            roleBtn.backgroundColor = RGBA(248, 248, 248, 1);
            roleBtn.enabled = NO;
            roleImgView.hidden = YES;
            editBackView.userInteractionEnabled = YES;
            InfoScrollView.contentSize = CGSizeMake(0, timeBtn.frame.origin.y + timeBtn.frame.size.height);
            break;
        default:
            break;
    }

}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KCHiddenKeyBoardNotification" object:nil];
}


#pragma mark --- uitextfieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self tapClick];
    return YES;
}

-(void)hiddenKeyboard
{
    [self tapClick];
    listTableView.hidden = YES;
}

-(void)BackClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(userInfoBack)]) {
        [_delegate userInfoBack];
    }
}


//通过communityId和timeKey(时间戳的字段名)来查询到时间戳
-(NSString *)getTimestamp:(NSString *)communityId timeKey:(NSString *)timeKey
{
    CommunityInfoLogic * timeStamp = [[CommunityInfoLogic alloc]init];
    NSString * str = [timeStamp getTimestamp:communityId timeKey:timeKey];
    return str;
}


//通过communityId和timeKey(时间戳的字段名)来更新时间戳
-(void)updateTimestamp:(NSString *)communityId  timeKey:(NSString *)timeKey timeKeyValue:(NSString *)timeKeyValue
{
   CommunityInfoLogic * timeStamp = [[CommunityInfoLogic alloc]init];
    [timeStamp updateTimestamp:communityId timeKey:timeKey timeKeyValue:timeKeyValue];
}


@end
